package com.study.homework;

public class MyHashSet {

    public static final int ARRAY_LENGTH = 1 << 10;

    public class Entity {
        int data;
        int hash;
        Entity next;
    }

    private Entity[] array = new Entity[ARRAY_LENGTH];

    public void put(int data) {
        Entity entity = getEntity(data);

        int index = getIndex(entity);

        Entity e = array[index];

        if (e == null) {
            array[index] = entity;
            return;
        }

        if (e.data == entity.data) {
            return;
        }

        while (e.next != null) {
            e = e.next;
            if (e.data == entity.data) {
                return;
            }
        }

        e.next = entity;
    }

    public boolean get(int data) {
        Entity entity = getEntity(data);
        int index = getIndex(entity);
        Entity e = array[index];

        if (e == null) {
            return false;
        }
        if (e.data == entity.data) {
            return true;
        }

        while (e.next != null) {
            e = e.next;
            if (e.data == entity.data) {
                return true;
            }
        }
        return false;
    }

    private int getIndex(Entity entity) {
        int data = entity.data;
        return data % ARRAY_LENGTH;
    }


    private Entity getEntity(int data) {
        Entity entity = new Entity();
        entity.data = data;
        entity.hash = data;
        entity.next = null;
        return entity;
    }

}
